package com.mini.framework.communicate.mina.adapter;

import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolDecoderOutput;
import org.apache.mina.filter.codec.demux.MessageDecoder;
import org.apache.mina.filter.codec.demux.MessageDecoderResult;

import com.mini.framework.communicate.mina.protocol.Decoder;
import com.mini.framework.communicate.mina.protocol.Msg;

public class MsgDecoderAdapter implements MessageDecoder {
	
	private Decoder decoder;
	
	public MsgDecoderAdapter(Decoder decoder){
		this.decoder = decoder;
	}

	public MessageDecoderResult decodable(IoSession session, IoBuffer byteBuffer) {
		return MsgDecoderResultAdapter.getDecodeResult(decoder.decodeAble(byteBuffer));
	}

	/**
	 * 解码.
	 * 
	 */
	public MessageDecoderResult decode(IoSession session, IoBuffer byteBuffer,
			ProtocolDecoderOutput out){
		MessageDecoderResult result = MessageDecoderResult.OK;
		Msg msg = decoder.decode(byteBuffer);
		if(msg != null){
			out.write(msg);
		}else{
			result = MessageDecoderResult.NEED_DATA;
		}
		return result;
	}

	public void finishDecode(IoSession arg0, ProtocolDecoderOutput arg1)
			throws Exception {

	}

}
